/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Connection.DataAccess;
import Model.HoaDon;

/**
 *
 * @author My PC
 */
public class HoaDonController {
    public static boolean ThemHD(HoaDon hd)
    {
        try 
        {
           String sql = "INSERT INTO tblHoaDon(MA_HOADON,MA_KHACHHANG,MA_NHANVIEN,"
                    + "NGAYLAP,GHICHU) "
                   + "values (N'"+hd.getMaHoadon()+"',N'"+hd.getMaKhachhang()+"',"
                   + "N'"+hd.getMaNhanvien()+"',"
                   + "'"+hd.getNgaylap()+"',N'"+hd.getGhichu()+"')";    
           DataAccess da=new DataAccess();
           da.executeUpdate(sql);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
     public static boolean XoaHD(HoaDon hd)
    {
        try 
        {
            
           String sql = "delete from tblHoaDon where MA_HOADON =N'"+hd.getMaHoadon()+"'";
           DataAccess da=new DataAccess();
           da.executeUpdate(sql);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
     
      public static boolean UpdTongTien(HoaDon hd)
     {
         try 
        {
              String sql = "UPDATE tblHoaDon SET TONGITEN = (SELECT SUM(THANHTIEN)"
                      + "FROM tblChiTietHoaDon WHERE MA_HOADON = N'"+hd.getMaHoadon()+"' "
                      + "GROUP BY MA_HOADON WHERE MA_HOADON = N'"+hd.getMaHoadon()+"'";
           DataAccess da=new DataAccess();
           da.executeUpdate(sql);
            return true;
        } catch (Exception e) {
            return false;
        } 
     }
      
    public static boolean SuaHD(HoaDon hd)
    {
        try 
        {
            
           String sql = "UPDATE tblHoaDon SET MA_NHANVIEN = N'"+hd.getMaNhanvien() +"',"
                   + " MA_KHACHHANG = N'"+hd.getMaKhachhang()+"',"
                   + "NGAYLAP = '"+hd.getNgaylap()+"', GHICHU = '"+hd.getGhichu()+"' WHERE "
                   + "MA_HOADON = N'"+hd.getMaHoadon()+"'";
           DataAccess da=new DataAccess();
           da.executeUpdate(sql);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    
}
